package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivitiesDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    //1、条件分页查询活动列表
    @Override
    public PageResult<Activity> listActivities(ActivitiesDto activitiesDto) {
//1、设置PageHelper参数
        PageHelper.startPage(activitiesDto.getPage(), activitiesDto.getPageSize());
//2、调用mapper接口查询

        List<Activity> activities = activityMapper.listActivities(activitiesDto);
//3、解析并封装分页结果
        Page<Activity> p = (Page<Activity>) activities;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
//2、添加活动
    @Override
    public void addActivity(Activity activity) {

        //设置创建时间
        activity.setCreateTime(LocalDateTime.now());
        //设置更新时间
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.addActivity(activity);
        log.info("添加活动：{}", activity);
    }
//根据id查询活动
    @Override
    public Activity getById(Integer id) {
        log.info("查询活动：{}", id);
        return activityMapper.getById(id);


    }
//修改活动
    @Override
    public void update(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
        log.info("修改活动：{}", activity);

    }
//查询指定类型的活动
    @Override
    public List<Activity> getActivitesByType(Integer type) {
        return activityMapper.getActivitesByType(type);

    }
//删除活动
    @Override
    public void delActivity(List<Integer> ids) {
        for (Integer id : ids) {
            activityMapper.deleteById(id);
        }

    }
//查询所有活动
    @Override
    public List<Activity> list() {
        log.info("查询所有活动");
        return activityMapper.list();


    }
//删除活动

}
